<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
String sysUserId = request.getParameter("sysUserId");
request.setAttribute("sysUserId",sysUserId); 
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body,html {
	margin: 0 auto;
	height: 100%;
	width: 100%;
}
</style>
</head>
<body>

 <DIV class="divfrist" id="system_add_user_department" style="width:100%;height:100%;overflow:auto;">
	<DIV id="" class="" style="width:0.4%;height:100%;float:left;overflow:hidden;"></DIV>
	<DIV id="" class="" style="width:99.2%;height:100%;float:left;overflow:hidden;;">
			<DIV id="" class="" style="float:left;width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" class="" style="float:left;width:100%;height:89%;">
				<DIV id="" class="allborder" style="height:100%;max-height:100%;max-width:100%;">
						<!-- 内容区 -->
						 <div style="width:100%;height:100%;overflow:auto;">
								  <ul id="addUserDepartment_tree" class="ztree"></ul>	
						 </div>		  
						<!-- 内容区 -->
					 </DIV>
			</DIV>
			<DIV id="" class="" style="float:left;width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" class="" style="float:left;width:100%;height:9%;">
				<DIV id="" class="allborder" style="height:100%;max-height:100%;max-width:100%;margin-top:-1px;">
							<!-- 内容区 -->
								<div style="width:100%;height:100%;padding:4px;">
									<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="window.user_department.addUserDepartment()">确认</a>
									<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="window.user_department.clearDepartment()">清空</a>
									[每次部门都丛新为用户分配所有部门]
								
								</div>
						<!-- 内容区 -->
					 </DIV>
			</DIV>
			<DIV id="" class="" style="float:left;width:100%;height:0.6%;overflow:hidden;"></DIV>

	</DIV>
	<DIV id="" class="" style="width:0.4%;height:100%;float:left;overflow:hidden;"></DIV>
 </DIV>
</body>
<script type="text/javascript">
(function(){
	//增加用户的部门
	function addUserDepartment(){
		var treeObj = $.fn.zTree.getZTreeObj("addUserDepartment_tree");
		var nodes = treeObj.getCheckedNodes(true);
		if(nodes.length<=0){
			alert("没有选中任何部门");
			return;
		}
		var arr = new Array();
		for(var i=0;i<nodes.length;i++){
			if(nodes[i].uuid==0){
				continue;
			}
			var a = new Object();
			a.departmentId=nodes[i].uuid;
			arr.push(a);
		}
		var alld = obj2str(arr);
		$.ajax({
			   type: "POST",
			   url:'${pageContext.request.contextPath}/sys/sysUserAction!addUserDepartment.action',
			   data: {rows1:alld,sysUserId:"${sysUserId}"},
			   success: function(msg){
			   		if(msg=="success"){
			   			alert("增加成功");
			   		}else{
			   			alert("增加失败");
			   		}
			   }
			});
	}
	//清空所有部门
	function clearDepartment(){
		var treeObj = $.fn.zTree.getZTreeObj("addUserDepartment_tree");
		treeObj.checkAllNodes(false);
		
	}
	//创建树
	function createTree(treeDate){
		var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				check: {
					enable: true,
					autoCheckTrigger: true
				}
		};
		if(treeDate==null||treeDate.length==0){
			$.ajax({
				   type: "POST",
				   url: "${pageContext.request.contextPath}/sys/sysUserAction!getUserDepartmentTree.action",
				   data:{sysUserId:"${sysUserId}"},
				   dataType:'json',
				   success: function(msg){
					   var node = new Object();
					   node.uuid=0;
					   node.id=1;
					   node.pid=0;
					   node.checked=true;
					   node.name="根部门";
					   node.open=true;
					   msg.push(node);
					   $.fn.zTree.init($("#addUserDepartment_tree"), setting, msg);     
				   }
			});	
		}else{
			 $.fn.zTree.init($("#addUserDepartment_tree"), setting, treeDate);    
		}
	}
	window.user_department={};
	window.user_department.addUserDepartment=addUserDepartment;
	window.user_department.clearDepartment=clearDepartment;
	$(function(){
		$.parser.parse('#system_add_user_department'); 
		 createTree();
	});
})();
</script>
</html>